package com.jdwx.opengl.face.dface;

import android.app.Application;
import android.content.SharedPreferences;

import com.dface.activation.Accredit;
import com.jdwx.opengl.face.utils.FaceConfig;
import com.jdwx.opengl.face.utils.JSONUtil;
import com.jdwx.opengl.face.utils.StringUtil;

/***
 * 初始化动态库
 */
 public class DFaceApplication extends Application {
    private static DFaceApplication instances;
    private Accredit accredit;

    public static DFaceApplication ins() {
        return instances;
    }

    public static FaceConfig faceConfig;
    @Override
    public void onCreate() {
        super.onCreate();
        instances = this;

        //加载jni动态库
        System.loadLibrary("dfacepro");

        //人脸识别模块配置加载
        String _faceConfig =  getString("FACE_CONFIG");
        if(StringUtil.isNullOrEmpty(_faceConfig)){
            faceConfig = new FaceConfig();
            setFaceConfig();
        }else{
            faceConfig = JSONUtil.DeserializeObject(_faceConfig,FaceConfig.class);
        }

        accredit = new Accredit();
    }

    /**
     * 是否激活
     * @return
     */
    public int isCheck(){
        return accredit.check();
    }

    /**
     * 设置人脸识别模块配置
     */
    public static void setFaceConfig(){
        String _faceConfig = JSONUtil.Serializable(faceConfig);
        setString("FACE_CONFIG",_faceConfig);

    }
    /**
     * 设置信息
     *
     * @param strKey
     * @param strValue
     */
    public static void setString(String strKey, String strValue) {
        SharedPreferences settings = instances.getSharedPreferences("UserInfo", 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(strKey, strValue);
        editor.commit();
    }

    /**
     * 设置信息
     *
     * @param strKey
     */
    public void removeString(String strKey) {
        SharedPreferences settings = instances.getSharedPreferences("UserInfo", 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.remove(strKey);
        editor.commit();
    }

    /**
     * 获取信息
     * @param strKey
     * @return
     */
    public String getString(String strKey) {
        SharedPreferences settings = instances.getSharedPreferences("UserInfo", 0);
        return settings.getString(strKey, "");
    }

}
